#include "tou.h"
#include "chuangjian.h"
#include "hanshu.h"
//主函数
void zhu(ElemType f){
//停车系统
    DNode* p = NULL;//必须为空指针
//跳跃点[停车系统]
HTXY:
    //清屏
    system("clear");
    printf("|————————————————————————商用停车场—————————————————————|\n");
    printf("|              进入管理系统(输入0)                      |\n");
    printf("|              入库停车(输入1)                          |\n");
    printf("|              离库缴费(输入2)                          |\n");
    printf("|              输入手机号对停车场车辆进行查询(输入3)    |\n");
    printf("|              退出停车系统(输入421)                    |\n");
    printf("|=======================================================|\n");
    //d(选择)a(车位数)
    ElemType d, a;
    printf("请输入您的选择: ");
    //排错
    ElemType lobster = scanf("%d", &d);
    if (0 == lobster){
        //将缓冲区中的杂质装起来
        char lobsterarr[1];
        //进行清空
        scanf("%s", lobsterarr);
        printf("异常输入\n");
        goto HTXY;
    }
        if(0 == d){
//跳跃点[进入管理系统]
HTXY0:
            //清屏
            system("clear");
            printf("进入管理系统\n");
            printf("请确保周围没人\n");
            sleep(1);
            //清屏
            system("clear");
            printf("是否进行回退\n是[0]否[1]");
            //s(是否进行回退)
            ElemType s;
            printf("请输入您的选择: ");
            //排错
            ElemType lobster0 = scanf("%d", &s);
            if (0 == lobster0){
                //将缓冲区中的杂质装起来
                char lobsterarr[1];
                //进行清空
                scanf("%s", lobsterarr);
                printf("异常输入\n");
                goto HTXY0;
            }
            if (0 == s){
                printf("正在退出管理系统, 请稍后\n");
                //循环
                ElemType j[10];
                for(ElemType i=0;i<10;i++){
                    j[i] = i;
                    }
                for (ElemType g = 9; g > -1; g--){
                        //清屏
                        system("clear");
                        printf("退出管理系统倒计时%d\n",j[g]);
                        sleep(1);
                    }
                //清屏
                system("clear");
                printf("已成功退出管理系统\n");
                sleep(1);
                //清屏
                system("clear");
                goto HTXY;
            }else if (1 == s){            
//跳跃点[输入密码]
HTXY01:
            sleep(1);
            //清屏
            system("clear");
            ElemType m;
            printf("输入密码: ");
            //排错
            ElemType lobster1 = scanf("%d", &m);
            if (0 == lobster1){
                //将缓冲区中的杂质装起来
                char lobsterarr[1];
                //进行清空
                scanf("%s", lobsterarr);
                printf("异常输入\n");
                goto HTXY01;
            }
            //清屏
            system("clear");
            sleep(1);
            if(520 == m){
//跳跃点[选择车库]
HTXY520:
                //清屏
                system("clear");
                sleep(1);
                printf("创建车库[输入1]\n");
                printf("回档原车库[输入2]\n");
                //车库选择
                ElemType asdf;
                //排错
                ElemType lobster3 = scanf("%d", &asdf);
                if (0 == lobster3){
                    //将缓冲区中的杂质装起来
                    char lobsterarr[1];
                    //进行清空
                    scanf("%s", lobsterarr);
                    printf("异常输入\n");
                    goto HTXY520;
                }
                if (1 == asdf){ 
//跳跃点[输入车库大小]
HTXY0520:
                //清屏
                system("clear");
                //创建车库
                printf("请输入数字, 输入其他后果自负\n");
                printf("输入车库大小：");
                //排错
                ElemType lobster2 = scanf("%d", &a);
                if (0 == lobster2){
                    //将缓冲区中的杂质装起来
                    char lobsterarr[1];
                    //进行清空
                    scanf("%s", lobsterarr);
                    printf("异常输入\n");
                    goto HTXY0520;
                }
                //清屏
                system("clear");
                for (ElemType i = 0; i < a; i++){
                    Tail(&p,i);
                    printf("车库%d车位创建成功\n", i);
                    sleep(1);
                }
                goto HTXY;
                }else if (2 == asdf){
//跳跃点[系统断电保护]
 HTXY05201:
                    //清屏
                    system("clear");
                    printf("系统断电保护\n");
                    printf("是否进行回档\n");
                    printf("YES/NO      0/1\n");
                    printf("输入选择:");
                    //选择
                    ElemType as;
                    //排错
                    ElemType lobster3 = scanf("%d", &as);
                        if (0 == lobster3){
                        //将缓冲区中的杂质装起来
                        char lobsterarr[1];
                        //进行清空
                        scanf("%s", lobsterarr);
                        printf("异常输入\n");
                        goto HTXY05201;
                    }
                    //断电前车库大小
                    printf("输入断电前车库大小:");
                    ElemType aas;
                    //排错
                    ElemType lobster4 = scanf("%d", &aas);
                        if (0 == lobster4){
                        //将缓冲区中的杂质装起来
                        char lobsterarr[1];
                        //进行清空
                        scanf("%s", lobsterarr);
                        printf("异常输入\n");
                        goto HTXY05201;
                    }
                    if (0 == as){
//跳跃点[输入回档一级密码]
HTXY521:
                        //清屏
                        system("clear");
                        printf("准备回档\n");
                        printf("输入回档一级密码: ");
                        //一级密码
                        ElemType htxy;
                        //排错
                        ElemType lobster4 = scanf("%d", &htxy);
                            if (0 == lobster4){
                            //将缓冲区中的杂质装起来
                            char lobsterarr[1];
                            //进行清空
                            scanf("%s", lobsterarr);
                            printf("异常输入\n");
                            goto HTXY521;
                        }
                        if (521 == htxy){      
//跳跃点[确保断电前车库大小]
HTXY12:
                        //清屏
                        system("clear");
                        printf("输入断电前车库大小\n");
                        //断电前车库大小
                        ElemType garage;
                        //排错
                        ElemType lobster5 = scanf("%d", &garage);
                            if (0 == lobster5){
                            //将缓冲区中的杂质装起来
                            char lobsterarr[1];
                            //进行清空
                            scanf("%s", lobsterarr);
                            printf("异常输入\n");
                            goto HTXY12;
                        }
                        if (garage == aas){
                        printf("确认无误[输入0]");
                        //确认
                        ElemType uin;
                        //排错
                        ElemType lobster5 = scanf("%d", &uin);
                        if (0 == lobster5){
                            //将缓冲区中的杂质装起来
                            char lobsterarr[1];
                            //进行清空
                            scanf("%s", lobsterarr);
                            printf("异常输入\n");
                            goto HTXY12;
                        }
                        if (0 == uin){
//跳跃点[输入回档二级密码]
HTXY_ZYT:
                            //清屏
                            system("clear");
                            printf("输入回档二级密码: ");
                            //二级密码
                            ElemType ZYT;
                            //排错
                            ElemType lobster6 = scanf("%d", &ZYT);
                                if (0 == lobster6){
                                //将缓冲区中的杂质装起来
                                char lobsterarr[1];
                                //进行清空
                                scanf("%s", lobsterarr);
                                printf("异常输入\n");
                                goto HTXY_ZYT;
                            }
                            if (421 == ZYT){
                                //清屏
                                system("clear");
                                printf("回退开启\n");
                                a = garage;
                                for (ElemType i = 0; i < a; i++){
                                Tail(&p,i);
                                printf("车库%d车位回档成功\n", i);
                                sleep(1);
                                }
                                load(p, a);
                                sleep(1);
                                printf("回退结束\n");
                                goto HTXY;
                            }else {
                                //清屏
                                system("clear"); 
                                sleep (1);
                                printf("二级密码错误，程序终止\n");
                                sleep (1);
                                //清屏
                                system("clear");        
                                goto HTXY;
                            }
                        }
                        }else{
                            //清屏
                            system("clear");
                            sleep(1);
                            printf("断电前车库大小错误, 拒接访问\n");
                            sleep (1);
                            //清屏
                            system("clear");
                            goto HTXY;
                        }
                        }else{
                            //清屏
                            system("clear"); 
                            sleep (1);
                            printf("一级密码错误，程序终止\n");
                            sleep (1);
                            //清屏
                            system("clear");        
                            goto HTXY;
                        }
                    }else if (1 == aas){
                        //清屏
                        system("clear");
                        printf("取消回档\n");
                        sleep(1);
                        goto HTXY;
                    }else{
                        //清屏
                        system("clear");
                        printf("输入错误, 重新输入\n");
                        sleep(1);
                        goto HTXY05201;
                    }
                //清屏
                system("clear");
                printf("创建结束\n");    
                sleep(1);
                goto HTXY;
                }else{
                    printf("错误输入\n");
                    goto HTXY520;
                }
                }else{
                    //清屏
                    system("clear"); 
                    sleep (1);
                    printf("密码错误，程序终止\n");
                    sleep (1);
                    //清屏
                    system("clear");        
                    goto HTXY;
                }
                }else{
                //清屏
                system("clear");
                printf("错误输入, 从新输入\n");
                sleep(1);
                goto HTXY0;
                }
        }else if(1 == d){
//跳跃点[请选择您的停车位]
parkingspace:
        //清屏
        system("clear");
        printf("进入车辆\n");
        printf("选择停车位\n");
        if (a -1 >= 0){
        printf("0--%d\n", a-1);
        //停车位(z)
        ElemType z;
        ElemType s = 1;
        //防止循环
        Dispatchsystem(p, a, &s);
        if (0 == s){
            goto HTXY;
        }
        printf("请选择您的停车位: ");
        //排错
        ElemType lobster3 = scanf("%d", &z);
        if (0 == lobster3){
            //将缓冲区中的杂质装起来
            char lobsterarr[1];
            //进行清空
            scanf("%s", lobsterarr);
            printf("异常输入\n");
            goto parkingspace;
        }
        sleep(1);
        ElemType s1 = 1;
        special(&p, z, a-1, &s1);
        if (0 == s1){
            goto parkingspace;
        }
        if (z >a-1){
            goto parkingspace;
        }
//跳跃点[是否选择停靠]
parkingspace1:
        //清屏
        system("clear");
        printf("是否选择停靠\n");
        //是否停靠(选择)
        ElemType h;
        printf("是[0]否[1]\n");
        printf("请输入您的选择: ");
        //排错
        ElemType lobster4 = scanf("%d", &h);
        if (0 == lobster4){
            //将缓冲区中的杂质装起来
            char lobsterarr[1];
            //进行清空
            scanf("%s", lobsterarr);
            printf("异常输入\n");
            goto parkingspace1;
        }
        size_t n;
        n = z;
        if (0 == h){
//跳跃点[请输入您的手机号]
parkingspace2:
            //清屏
            system("clear");
            ElemType g;
            printf("请输入您的手机号: ");
            //排错
            ElemType lobster5 = scanf("%d", &g);
            if (0 == lobster5){
                //将缓冲区中的杂质装起来
                char lobsterarr[1];
                //进行清空
                scanf("%s", lobsterarr);
                printf("异常输入\n");
                goto parkingspace2;
            }
            Storage(&p, n, g);
            printf("车库已开启\n");
            sleep(1);
            goto HTXY;
        }else if (1 == h){
            //清屏
            system("clear");
            sleep(1);
            printf("欢迎再来\n");
            sleep(1);
            goto HTXY;
        }
        }else{
        sleep(1);
        //清屏
        system("clear");
        printf("还没停车场\n");
        sleep(1);
        goto HTXY;
        } 
        }else if(2 == d){
//跳跃点[请输入手机号]
parkingspace3:
            //清屏
            system("clear");
            //手机号
            ElemType m;
            printf("请输入手机号\n");
            //排错
            ElemType lobster6 = scanf("%d", &m);
            if (0 == lobster6){
                //将缓冲区中的杂质装起来
                char lobsterarr[1];
                //进行清空
                scanf("%s", lobsterarr);
                printf("异常输入\n");
                goto parkingspace3;
            }
            //清屏
            system("clear");
            printf("正在查询车辆\n");
            sleep(1);
            //找到车辆所在的首地址
            ElemType d, w;
            ElemType s=1;
            lookfor(&p, m,&s);
            if (s == 0)
            {
                goto HTXY;
            }
//跳跃点[输入停车时长]
toll:
            //清屏
            system("clear");
            printf("请车主抬起右手摸着良心输入停车时间\n");
            printf("输入停车时长:  h\n");
            //排错
            ElemType lobster7 = scanf("%d", &d);
            if (0 == lobster7){
                //将缓冲区中的杂质装起来
                char lobsterarr[1];
                //进行清空
                scanf("%s", lobsterarr);
                printf("异常输入\n");
                goto toll;
            }
            //清屏
            system("clear");
            printf("停车时长%dh\n", d);
            if (0 > d){
                printf("请正确输入停车时间\n");
                sleep(1);
                goto toll;
            }else if (1 >= d){
                //清屏
                system("clear");
                printf("请缴费0元\n");
                Paythemoney(&p, m);
                printf("祝您一路顺风\n");
                sleep(1);
                goto HTXY;
            }else if (12 > d && 1 < d){
//跳跃点[请缴费%d元\n", d*5]
toll1:
                //清屏
                system("clear");
                sleep(1);
                printf("请缴费%d元\n", d*5);
                ElemType q;
                //排错
                ElemType lobster8 = scanf("%d", &q);
                if (0 == lobster8){
                    //将缓冲区中的杂质装起来
                    char lobsterarr[1];
                    //进行清空
                    scanf("%s", lobsterarr);
                    printf("异常输入\n");
                    goto toll1;
                }
                if(d*5 == q){
                    //清屏
                    system("clear");
                    Paythemoney(&p, m);
                    sleep(1);
                    goto HTXY;
                }else{
                    //清屏
                    system("clear");
                    printf("钱包余额不足，交易取消\n");
                    sleep(1);
                    goto toll;
                }
            }else if (24 > d && 12 <= d){
//跳跃点[请缴费88元]
toll2:
            sleep(1);
            ElemType q, w;
            printf("请缴费88元\n");
            //排错
            ElemType lobster9 = scanf("%d", &q);
            if (0 == lobster9){
                //将缓冲区中的杂质装起来
                char lobsterarr[1];
                //进行清空
                scanf("%s", lobsterarr);
                printf("异常输入\n");
                goto toll2;
            }
            if (88 == q){
                sleep(1);
                //清屏
                system("clear");
                Paythemoney(&p, m);
                sleep(1);
                goto HTXY;
            }else{
                sleep(1);
                //清屏
                system("clear");
                printf("钱包余额不足，交易取消\n");
                sleep(1);
                goto toll;
            }
            }else if(24 <= d){
                sleep(1);
                //清屏
                system("clear");  
                printf("停车超时，已转人工\n");
                sleep(1);
                goto HTXY;
                }
        }else if(3 == d){
//跳跃点[请输入要查车辆车主的手机号]
toll3:
            //清屏
            system("clear");
            ElemType m;
            printf("请输入要查车辆车主的手机号:");
            ElemType s=1;
            //排错
            ElemType lobster10 = scanf("%d", &m);
            if (0 == lobster10){
                //将缓冲区中的杂质装起来
                char lobsterarr[1];
                //进行清空
                scanf("%s", lobsterarr);
                printf("异常输入\n");
                goto toll3;
            }
            Catchtheadulterer(&p, m,&s);
            if (0 == s)
            {
                goto HTXY;
            }
            printf("\n");
        }else if(421 == d){
            //清屏
            system("clear");
            printf("欢迎再次使用\n");
            ElemType n = countDNode(p);
            Save(p, n);
            printf("进入关机倒计时\n");
            ElemType j[10];
            for(ElemType i=0;i<10;i++){
                j[i] = i;
                }
            for (ElemType a = 9; a > -1; a--){
                    //清屏
                    system("clear");
                    printf("关机倒计时%d\n",j[a]);
                    sleep(1);
                }
            //清屏
            system("clear");
            perror("欢迎再次使用\n");
            sleep(1);
            return;
            }else{
                //清屏
                system("clear");
                printf("输入错误, 请重新选择服务\n");
                sleep(1);
                goto HTXY;
            }
            //清屏
            system("clear");
}
//通过数据查找下标
ElemType indexNdoe(DNode* p, ElemType data){
    DNode* htxy =p;
    int count = 0;
    while (htxy){
        if (htxy ->data == data){
            return count;
        }
        if (NULL == htxy ->prior){
            return -1;
        }
        htxy = htxy ->prior;
        count ++;
    }
    return -1;
}
//捉奸车辆寻找系统
void Lookforasystem(DNode* p, ElemType num){
    DNode* head = p;
    //清屏
    system("clear");
    printf("该车辆在%d号停车位上\n", indexNdoe(head, num));
    sleep(1);
    return;
}
//捉奸系统
void Catchtheadulterer(DNode** p, ElemType findData, ElemType* s){
    DNode* newParkingspaces = NULL;
    //无效参数
    if (NULL == p){
    printf("数据有误，程序终止\n");
    sleep(1);
    return;
    }
    //清屏
    system("clear");
    //利用手机号s对车位进行查找
    newParkingspaces = DNodehaed(*p, findData);
    //当输入的手机号的车辆不在停车场内
    if (NULL == newParkingspaces){
        //清屏
        system("clear");
        printf("无此车辆\n");
        sleep(1);
        *s = aaa();
    }else{
//跳跃点[捉奸系统]
Catchtheadulterer:
        //清屏
        system("clear");
        printf("车辆存在， 是否进行停车位查询\n[是/否]————————[0/1]\n");
        //判断语句
        ElemType asdf;
        //排错
        ElemType lobster11 = scanf("%d", &asdf);
        if (0 == lobster11){
            //将缓冲区中的杂质装起来
            char lobsterarr[1];
            //进行清空
            scanf("%s", lobsterarr);
            printf("异常输入\n");
            sleep(1);
            goto Catchtheadulterer;
        }
        Lookforasystem(*p, findData);
        sleep(1);
        *s = aaa();
    }
}
//跳转函数
int aaa(){
    return 0;
}
//车位调度系统
void Dispatchsystem(DNode* p, ElemType a, ElemType* s){
    //清屏
    system("clear");
    if(NULL == p){
    printf("调度失败, 程序终止\n");
    sleep(1);
    *s = aaa();
    return;
    }
    sleep(1);
    printf("开始调度\n");
    for (ElemType i = 0; i < a; i++)
    {
        if (0 == p ->data){
            printf("%d号停车位上未停车\n", i);
        }else{
            printf("%d号停车位上已停车\n", i);
        }
        p = p ->prior;
    }printf("调度完毕\n");
}
//车辆离库
void Paythemoney(DNode** p, size_t findData){
    DNode* q = DNodehaed(*p, findData);
    //无效参数
    if (NULL == q){
    printf("数据有误，程序终止\n");
    sleep(2);
    return;
    }
    //清屏
    system("clear");
    printf("正在查找车辆, 请稍等\n");
    sleep(1);
    for (ElemType i = 0; i < 5; i++){
        //清屏
        system("clear");
        printf("正在交费中: %d", i);
        printf("\n");
        sleep(1);
    }
    //对车位节点重新赋值为0, 准备下一次车辆进库
    q -> data = 0;
    DNode* x = DNodehaed(*p, findData);
    if (NULL == x){
        printf("已完成缴费, 祝您一路顺风\n");
    }
}
//在链表中找数据    通过偏移量来找    返回节点首地址   找不到 返回NULL
DNode* findNodeByPos(DNode* p,size_t num){
    //无效参数
    if (NULL == p){
        printf("数据有误，程序终止\n");
        sleep(1);
        return NULL;
    }
    //无效参数
    if (num < 0){
        printf("数据有误，程序终止\n");
        sleep(1);
        return NULL;
    }  
    for(int i=0;i<num;i++){
        p = p->prior;
        if(NULL == p) return NULL;
    }
    return p;
}
//车辆入库
void Storage(DNode** p, size_t findData, ElemType num){
    DNode* q = findNodeByPos(*p, findData);
    //无效参数
    if (NULL == q){
    printf("数据有误，程序终止\n");
    sleep(1);
    return;
    }
    //清屏
    system("clear");
    printf("正在启动车位, 请稍等\n");
    sleep(1);
    for (ElemType i = 0; i < 5; i++){
        //清屏
        system("clear");
        printf("正在创建中: %d", i);
        printf("\n");
        sleep(1);
    }
    //将车位节点赋值为手机号
    q -> data = num;
}
//在链表中找数据,通过数据来找返回节点首地址,找不到返回NULL
DNode* DNodehaed(DNode* p,int findData){
    //无效参数
    if (NULL == p){
    printf("数据有误，程序终止\n");
    sleep(1);
    return NULL;
    }
    while(p){//遍历整个链表
        if(findData == p->data) return p;
        p= p->prior;
    }
    return p;
}
//特殊寻找
void special(DNode** p, ElemType findData, ElemType parkingspace, ElemType* s){
    DNode* newParkingspaces = NULL;//必须为空指针
    //无效参数
    if (NULL == p){
    printf("数据有误，程序终止\n");
    sleep(1);
    return;
    }
    //当输入停车位大于停车场车位范围
    if (findData > parkingspace){
        //清屏
        system("clear");
        printf("以超出停车位范围, 请重新输入\n");
        sleep(1);
        return;
    }
    //寻找车位
    newParkingspaces = findNodeByPos(*p, findData);
    //判断此车位
        if(NULL != newParkingspaces){
            //遍历当前节点上有无车辆
            //当数据域中为0则没车， 不为0则有车 
            if(0 == newParkingspaces ->data){
                printf("此车位无车, 可以停靠\n");
                sleep(1);
            }else{
                printf("车位有车, 不可停靠\n");
                sleep(1);
                *s = aaa();
            }
        }else{
            printf("无此车位, 请重新输入\n");
            sleep(1);
            *s = aaa();
        }
}
//寻找车辆
void lookfor(DNode** p, ElemType findData, ElemType* s){
    //无效参数
    if (NULL == p){
    printf("数据有误，程序终止\n");
    sleep(1);
    return;
    }
    //寻找车辆
    DNode* Parkingspaces = DNodehaed(*p, findData);
        if(NULL == Parkingspaces){
        //清屏
        system("clear");
        sleep(1);
        printf("请输入正确手机号\n");
        sleep(1);
        *s = aaa();
        }
}
//将数据从Power-down protection.txt中取出
void load(DNode* p, ElemType n){
    FILE* fp;
    DNode* q = p;
    if (NULL == (fp = fopen("Power-down protection.txt", "r"))){
        //清屏
        system("clear");
        printf("读取文档失败, 复写终止\n");
        sleep(1);
        return;
    }else{
        //清屏
        system("clear");
        printf("读取文档成功, 复写开始\n");
        sleep(1);
    }
    while (n)
    {
        fscanf(fp, "%d\n", &q ->data);
        q = q ->prior;
        if ( NULL == q){
        break;
        }
    }
    //清屏
    system("clear");
    printf("复写结束\n");
    sleep(1);
    //关闭文档
    fclose(fp);
}
//将数据存入Power-down protection.txt中
void Save(DNode* p, ElemType n){
   //无效参数
    if (NULL == p){
    printf("数据有误，程序终止\n");
    sleep(1);
    return;
    }
    //读取文档
    FILE* fp;
    DNode* q = p;
    if (NULL == (fp = fopen("Power-down protection.txt", "w+"))){
        //清屏
        system("clear");
        printf("读取文档失败, 存入终止\n");
        sleep(1);
        return;
    }else{
        //清屏
        system("clear");
        printf("读取文档成功, 存入开始\n");
        sleep(1);
    }
    printf("%d\n", n);
    for(ElemType i = 0;i < n;i++){
        fprintf (fp, "%d\n", q ->data);
        q = q ->prior;
        if ( NULL == q){
            break;
        }
    }
    //清屏
    system("clear");
    printf("存档成功\n");
    sleep(1);
    //关闭文档
    fclose (fp); 
}
//计算单链表节点个数
ElemType countDNode(struct dNode* head){
    //计数器count用来记录节点个数
    ElemType count = 0;
    struct dNode* currentDNode = head;
    while (NULL != currentDNode){
        count ++;
        currentDNode = currentDNode ->prior;
    }
    return count;
}